package com.andyliu.sys.service.impl;

import com.andyliu.sys.mapper.SActionLogMapper;
import com.andyliu.sys.pojo.SActionLog;
import com.andyliu.sys.pojo.SActionLogExample;
import com.andyliu.sys.service.LogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * ClassName: LogServiceImpl
 * Description: TODO
 * date: 2021/1/21 3:21 下午
 *
 * @author andyliu
 * @since JDK 1.8
 */
@Service
public class LogServiceImpl implements LogService {
   @Autowired
   private SActionLogMapper actionLogMapper;
    @Override
    public void saveActionLog(SActionLog log) {
        actionLogMapper.insertSelective(log);
    }

    @Override
    public PageInfo<SActionLog> queryAactionLogByPage(SActionLog log, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        SActionLogExample ex = new SActionLogExample();
        SActionLogExample.Criteria criteria = ex.createCriteria();
        if(log!=null){
            if(log.getUrl()!=null && !"".equals(log.getUrl())){
                criteria.andUrlLike("%"+log.getUrl()+"%");
            }
        }
        ex.setOrderByClause(" id desc");
        List<SActionLog> datas = actionLogMapper.selectByExample(ex);
        return new PageInfo<SActionLog>(datas);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean deleteActionLogs(Integer[] ids) {
        boolean status = false;
        int flag =0;
        for(Integer id:ids){
            flag+=actionLogMapper.deleteByPrimaryKey(id);
        }
        if(flag==ids.length){
            status = true;
        }
        return status;
    }
}
